package yl.hs.bmipfull.controllers.production;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.production.IProductionRecordService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 张凯
 * @apiNote 生产记录表控制层
 * @since 2021年10月25日 15时53分41秒
 */
@Api(value = "生产记录表Web接口", tags = "生产管理——生产记录表操作")
@RequestMapping(value = "/api/productionRecord")
@RestController
public class ProductionRecordController extends ControllerBase {

    @Autowired
    IProductionRecordService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询生产记录表", notes = "查询生产记录表")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询生产记录表", notes = "分页查询生产记录表")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPageVO(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询生产记录表", notes = "分页查询生产记录表")
    @RequestMapping(value = "/querybytruckplate/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryByTruckPlate(@RequestBody PageQuery query,
                                       @ApiParam(value = "页码") @PathVariable int page,
                                       @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryProductionRecordSModel(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询生产记录明细表", notes = "查询生产记录明细表")
    @RequestMapping(value = "/querydetails", method = RequestMethod.GET)
    public WebResult queryDetails(@RequestParam List<Long> allId) {
        try {
            var result = service.queryByRecordId(allId);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询生产记录表", notes = "根据生产记录表编号查询生产记录表")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存生产记录表", notes = "查询/更新生产记录表信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveProductionRecord(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveProductionRecord(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }



}